#------------------------------------------------------------------------------
# Project:     Midterm Assignment
# Name:        Ball.py
# Purpose:     Bouncing Ball Object
# 
# Author:     Sam Milender
#
# Created:     1/15/2013
# Copyright:   (c) Sam Milender 2012
# Licence:     New BSD
#------------------------------------------------------------------------------
import pygame, math, sys, random

class Ball():
    def __init__(self, images, speed, position, waitMax = 100):
        self.surfaces = []
        for image in images:
            self.surfaces += [pygame.image.load(image)]
#            taken from my original ball code
        self.frame = 0
        self.maxFrame = len(self.surfaces)-1
        self.surface = self.surfaces[self.frame]
        self.rect = self.surface.get_rect()
        self.speed = speed
        self.place(position)
        self.living = True
        self.waitCount = 0
        self.waitMax = waitMax
        self.radius = self.rect.width/2
        if pygame.mixer:
            self.bounceSound = pygame.mixer.Sound("bounce.wav")
        
    def  __str__(self):
        pass
#        return "I'm a ball " + str(self.rect.center) + str(self.speed) + str(self.living)
     
    def place(self, pt):
#        print "I've moved to", pt
        self.rect = self.rect.move(pt)
#        taken from my original ball code
        
    def move(self):
#        print "I've moved", self.speed
        self.rect = self.rect.move(self.speed)
        
        
#            taken from my original ball code
        
    def distToPoint(self, pt):
        x1 = self.rect.center[0]
        x2 = pt[0]
        y1 = self.rect.center[1]
        y2 = pt[1]
        return math.sqrt(((x2-x1)**2)+((y2-y1)**2))
#        taken from my original ball code
        
    def collideFloor(self, screenHeight):
#        print "trying to hit bottom of screen", screenHeight
        if (self.rect.bottom > screenHeight):
            self.living = False
#            taken from my original ball code
    
    def collideWall(self, screenWidth, screenHeight):
#        print "trying to hit edges of screen", screenWidth, screenHeight
        if (self.rect.left < 0 
            or self.rect.right > screenWidth):
            self.speed[0] = self.speed[0]*-1
        if (self.rect.top < 0):
            self.speed[1] = self.speed[1]*-1
#        taken from my original ball code
            
            
            
    def collidePaddle(self, paddle):
#        print "trying to hit paddle", str(paddle)
        if (self.rect.right > paddle.rect.left 
            and self.rect.left < paddle.rect.right):
            if (self.rect.bottom > paddle.rect.top):
                    self.speed[1] = self.speed[1] * -1
                    if self.frame < self.maxFrame:
                        self.frame += 1 
                    else:
                        self.frame = 0
                    self.surface = self.surfaces[self.frame]
        #                    taken from my original ball code and edited
        
    def collideBall(self, other):
#        print "trying to hit ", str(other)
        if (self.rect.right > other.rect.left 
            and self.rect.left < other.rect.right):
            
            if (self.rect.bottom > other.rect.top and 
                self.rect.top < other.rect.bottom):
                
                if (self.distToPoint(other.rect.center)
                    < self.radius + other.radius): 
                     
                    self.speed[0] = self.speed[0] * -1
                    self.speed[1] = self.speed[1] * -1
                    other.speed[0] = other.speed[0] * -1
                    other.speed[1] = other.speed[1] * -1
#        taken from my original ball code